package org.jeecg.modules.hbjh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocom.api.DefaultBocomClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.hbjh.config.BCMApiConfig;
import org.jeecg.modules.hbjh.convert.TaxResponseRtnConvert;
import org.jeecg.modules.hbjh.requestVo.DocumentPushCallbackRequestV1;
import org.jeecg.modules.hbjh.requestVo.FullElcReceiptBackfillRequestV1;
import org.jeecg.modules.hbjh.requestVo.ReceiptBackfillRequestV1;
import org.jeecg.modules.hbjh.responseVo.DocumentPushCallbackResponseV1;
import org.jeecg.modules.hbjh.responseVo.FullElcReceiptBackfillResponseV1;
import org.jeecg.modules.hbjh.responseVo.ReceiptBackfillResponseV1;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
* @Description: 回调河北交通银行数据转换接口
* @Author: jeecg-boot
* @Date:   2023-08-02
* @Version: V1.0
*/
@Api(tags="回调河北交通银行数据转换接口")
@RestController
@RequestMapping("/convert/response")
@Slf4j
public class HbjhResponseConvertController {
    /**
     * 转发调用地址
     */
    public enum CONVERTURL {
        djtsCallback ("单据推送结果回调地址",BCMApiConfig.APIGW_URL_ADDRESS + "/api/acissmbp/documentPushCallback/v1"),
        fpmxCallback ("发票明细回填回调地址",BCMApiConfig.APIGW_URL_ADDRESS + "/api/acissmbp/receiptBackfill/v1"),
        qdpsqCallback ("全电票正票申请结果回调地址",BCMApiConfig.APIGW_URL_ADDRESS + "/api/acissmbp/fullElcReceiptBackfill/v1");
        private final String desc;//描述
        private final String url;//url地址
        CONVERTURL (String desc, String url) {
            this.desc = desc;
            this.url = url;
        }
    }
    /**
     * 销项-单据推送结果回填
     * @param params
     * @param
     * @return
     */
    @AutoLog(value = "回调河北交通银行数据转换接口-单据推送结果回填")
    @ApiOperation(value="回调河北交通银行数据转换接口-单据推送结果回填", notes="回调河北交通银行数据转换接口-单据推送结果回填")
    @PostMapping(value = "/djtsCallback")
    public JSONObject djtsCallback(@RequestBody Result<JSONArray> params) {
        String code="-1",msg="回填失败";
        JSONObject rtn=new JSONObject();
        try {
            DefaultBocomClient client = new DefaultBocomClient(BCMApiConfig.APP_ID, BCMApiConfig.MY_PRIVATE_KEY, BCMApiConfig.APIGW_PUBLIC_KEY);
            /**
             * 测试环境可以忽略SSL证书告警，生产环境不可忽略
             */
            DocumentPushCallbackRequestV1 request = new DocumentPushCallbackRequestV1();
            request.setServiceUrl(CONVERTURL.djtsCallback.url);
            //单据推送结果转换
            JSONObject jsonObject = TaxResponseRtnConvert.djtsRtnConvert(params);
            DocumentPushCallbackRequestV1.DocumentPushCallbackRequestV1Biz bizContent=JSONObject.parseObject(jsonObject.toJSONString(),DocumentPushCallbackRequestV1.DocumentPushCallbackRequestV1Biz.class);
            bizContent.setReqorgcode(BCMApiConfig.reqOrgCode);
            request.setBizContent(bizContent);
            DocumentPushCallbackResponseV1 response = client.execute(request,UUID.randomUUID().toString().replace("-", ""));
            if (response.isSuccess()) {
                code="200";
                msg="回填成功";
                log.info(response.toString());
            } else {
                msg="回填失败："+response.getRspMsg();
            }
        }catch (Exception e){
            e.printStackTrace();
            msg="回填失败："+e.getMessage();
        }finally {
            rtn.put("code",code);
            rtn.put("msg",msg);
            return rtn;
        }
    }

    /**
     * 销项-发票明细回填
     * @param params
     * @param
     * @return
     */
    @AutoLog(value = "回调河北交通银行数据转换接口-发票明细回填")
    @ApiOperation(value="回调河北交通银行数据转换接口-发票明细回填", notes="回调河北交通银行数据转换接口-发票明细回填")
    @PostMapping(value = "/fpmxCallback")
    public JSONObject fpmxCallback(@RequestBody JSONObject  params) {
        String code="-1",msg="回填失败";
        JSONObject rtn=new JSONObject();
        try {
            DefaultBocomClient client = new DefaultBocomClient(BCMApiConfig.APP_ID, BCMApiConfig.MY_PRIVATE_KEY, BCMApiConfig.APIGW_PUBLIC_KEY);
            /**
             * 测试环境可以忽略SSL证书告警，生产环境不可忽略
             */
            ReceiptBackfillRequestV1 request = new ReceiptBackfillRequestV1();
            request.setServiceUrl(CONVERTURL.fpmxCallback.url);
            //数据转换
            JSONObject jsonObject = TaxResponseRtnConvert.fpmxRtnConvert(params);
            System.out.println("回调河北交通银行数据转换接口-发票明细回填-转换后请求参数：" + JSONObject.toJSONString(jsonObject));
            ReceiptBackfillRequestV1.ReceiptBackfillRequestV1Biz  bizContent=JSONObject.parseObject(jsonObject.toJSONString(),ReceiptBackfillRequestV1.ReceiptBackfillRequestV1Biz.class);
            bizContent.setReqOrgCode(BCMApiConfig.reqOrgCode);
            request.setBizContent(bizContent);
            ReceiptBackfillResponseV1 response = client.execute(request,UUID.randomUUID().toString().replace("-", ""));
            if (response.isSuccess()) {
                code="200";
                msg="回填成功";
                log.info(response.toString());
            } else {
                msg="回填失败："+response.getRspMsg();
            }
        }catch (Exception e){
            e.printStackTrace();
            msg="回填失败："+e.getMessage();
        }finally {
            rtn.put("code",code);
            rtn.put("msg",msg);
            return rtn;
        }
    }
    /**
     * 销项-全电票正票申请结果回填
     * @param params
     * @param
     * @return
     */
    @AutoLog(value = "回调河北交通银行数据转换接口-全电票正票申请结果回填")
    @ApiOperation(value="回调河北交通银行数据转换接口-全电票正票申请结果回填", notes="回调河北交通银行数据转换接口-全电票正票申请结果回填")
    @PostMapping(value = "/qdpsqCallback")
    public JSONObject qdpsqCallback(@RequestBody JSONObject  params) {
        String code="-1",msg="回填失败";
        JSONObject rtn=new JSONObject();
        try {
            DefaultBocomClient client = new DefaultBocomClient(BCMApiConfig.APP_ID, BCMApiConfig.MY_PRIVATE_KEY, BCMApiConfig.APIGW_PUBLIC_KEY);
            /**
             * 测试环境可以忽略SSL证书告警，生产环境不可忽略
             */
            client.ignoreSSLHostnameVerifier();

            FullElcReceiptBackfillRequestV1 request = new FullElcReceiptBackfillRequestV1();
            request.setServiceUrl(CONVERTURL.qdpsqCallback.url);
            //转换返回结果
            JSONObject jsonObject = TaxResponseRtnConvert.fpkjAsyncRtnConvert(params);
            System.out.println("回调河北交通银行数据转换接口-全电票正票申请结果回填-转换后请求参数：" + JSONObject.toJSONString(jsonObject));
            FullElcReceiptBackfillRequestV1.FullElcReceiptBackfillRequestV1Biz  bizContent=JSONObject.parseObject(jsonObject.toJSONString(),FullElcReceiptBackfillRequestV1.FullElcReceiptBackfillRequestV1Biz.class);
            bizContent.setReqorgcode(BCMApiConfig.reqOrgCode);
            request.setBizContent(bizContent);
            FullElcReceiptBackfillResponseV1 response = client.execute(request,UUID.randomUUID().toString().replace("-", ""));
            if (response.isSuccess()) {
                code="200";
                msg="回填成功";
                log.info(response.toString());
            } else {
                msg="回填失败："+response.getRspMsg();
            }
        }catch (Exception e){
            e.printStackTrace();
            msg="回填失败："+e.getMessage();
        }finally {
            rtn.put("code",code);
            rtn.put("message",msg);
            return rtn;
        }
    }
}
